package com.shtb.wisdom.videotaker

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View

class RecordButton(
    context: Context?,
    attrs: AttributeSet?
) : View(context, attrs) {
    private var progress = 0f

    //画圆所在的距形区域
    var oval: RectF
    var paint: Paint
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        var width = this.width
        var height = this.height
        if (width != height) {
            val min = Math.min(width, height)
            width = min
            height = min
        }
        paint.isAntiAlias = true // 设置画笔为抗锯齿
        paint.color = Color.WHITE // 设置画笔颜色
        canvas.drawColor(Color.TRANSPARENT) // 白色背景
        val progressStrokeWidth = 18
        paint.strokeWidth = progressStrokeWidth.toFloat() //线宽
        paint.strokeCap = Paint.Cap.ROUND
        paint.style = Paint.Style.STROKE
        oval.left = progressStrokeWidth / 2.toFloat() // 左上角x
        oval.top = progressStrokeWidth / 2.toFloat() // 左上角y
        oval.right = width - progressStrokeWidth / 2.toFloat() // 左下角x
        oval.bottom = height - progressStrokeWidth / 2.toFloat() // 右下角y
        paint.color = Color.GREEN
        canvas.drawArc(oval, -90f, progress * 360, false, paint) // 绘制进度圆弧，这里是蓝色
    }

    fun setProgress(progress: Float) {
        this.progress = progress
        this.invalidate()
    }

    init {
        oval = RectF()
        paint = Paint()
    }
}